package com.blobfish.fiction.common.base.model;

import cn.hutool.core.util.StrUtil;
import com.blobfish.fiction.common.base.exception.BaseErrorException;
import com.blobfish.fiction.common.base.knowledge.IMessageEnum;
import com.jfinal.kit.PropKit;

/**
 * 结果集
 *
 * @param <T>
 * @author huangbo
 */
public class QmResult<T> extends BaseDto {
    private static final long serialVersionUID = 1L;
    private Integer code = 0;
    private String msgId;
    private String msg;
    private boolean needRefresh;
    private T data;

    public QmResult() {
        this.code = 200;
    }

    public QmResult(T data) {
        this.data = data;
    }

    public QmResult(Integer code, String msgId, String msg, T data) {
        this.code = code;
        if (StrUtil.isBlank(msgId)) {
            this.code = 200;
        } else {
            this.code = 500;
        }
        this.msgId = msgId;
        this.msg = msg;
        this.data = data;
        this.needRefresh = false;
    }

    public QmResult(BaseErrorException error) {
        this.code = 500;
        this.msgId = error.getId();
        this.msg = error.getMessage();
    }

    public QmResult(IMessageEnum msgId) {
        this(msgId, (Object[]) null);
    }

    public QmResult(IMessageEnum msgId, Object[] args) {
        this.code = 500;
        this.msgId = msgId.getId();
        String messageTemplate = PropKit.use("message.txt").get(this.msgId);
        if (StrUtil.isNotEmpty(messageTemplate) && args != null && args.length > 0) {
            messageTemplate = StrUtil.format(messageTemplate, args);
        }

        this.msg = messageTemplate;
    }

    public void converter(QmResult<?> result) {
        this.msgId = result.getMsgId();
        this.code = result.getCode();
        this.msg = result.getMsg();
    }

    public void setMsgId(String msgId) {
        this.msgId = msgId;
        this.code = 500;
    }

    public boolean isNeedRefresh() {
        return needRefresh;
    }

    public void setNeedRefresh(boolean needRefresh) {
        this.needRefresh = needRefresh;
    }

    public static <T> QmResult.QmResultBuilder<T> builder() {
        return new QmResult.QmResultBuilder();
    }


    @Override
    public boolean equals(Object o) {
        if (o == this) {
            return true;
        } else if (!(o instanceof QmResult)) {
            return false;
        } else {
            QmResult<?> other = (QmResult) o;
            if (!other.canEqual(this)) {
                return false;
            } else if (!this.getCode().equals(other.getCode())) {
                return false;
            } else {
                label61:
                {
                    Object this$msgId = this.getMsgId();
                    Object other$msgId = other.getMsgId();
                    if (this$msgId == null) {
                        if (other$msgId == null) {
                            break label61;
                        }
                    } else if (this$msgId.equals(other$msgId)) {
                        break label61;
                    }

                    return false;
                }

                label54:
                {
                    Object this$msgText = this.getMsg();
                    Object other$msgText = other.getMsg();
                    if (this$msgText == null) {
                        if (other$msgText == null) {
                            break label54;
                        }
                    } else if (this$msgText.equals(other$msgText)) {
                        break label54;
                    }

                    return false;
                }

                Object this$data = this.getData();
                Object other$data = other.getData();
                if (this$data == null) {
                    if (other$data != null) {
                        return false;
                    }
                } else if (!this$data.equals(other$data)) {
                    return false;
                }
                return true;
            }
        }
    }

    @Override
    protected boolean canEqual(Object other) {
        return other instanceof QmResult;
    }

    @Override
    public String toString() {
        return "QmResult(code=" + this.getCode() + ", msgId=" + this.getMsgId() + ", msg=" + this.getMsg() + ", data=" + this.getData() + ")";
    }

    private void setStatus(Integer code) {
        this.code = code;
    }

    public static class QmResultBuilder<T> {
        private boolean status;
        private String msgId;
        private String msgText;
        private T data;
        private String encryptData;

        QmResultBuilder() {
        }

        public QmResult.QmResultBuilder<T> status(boolean status) {
            this.status = status;
            return this;
        }

        public QmResult.QmResultBuilder<T> msgId(String msgId) {
            this.msgId = msgId;
            return this;
        }

        public QmResult.QmResultBuilder<T> msgText(String msgText) {
            this.msgText = msgText;
            return this;
        }

        public QmResult.QmResultBuilder<T> data(T data) {
            this.data = data;
            return this;
        }

        public QmResult.QmResultBuilder<T> encryptData(String encryptData) {
            this.encryptData = encryptData;
            return this;
        }

        @Override
        public String toString() {
            return "QmResult.QmResultBuilder(status=" + this.status + ", msgId=" + this.msgId + ", msgText=" + this.msgText + ", data=" + this.data + ", encryptData=" + this.encryptData + ")";
        }
    }

    public Integer getCode() {
        return code;
    }

    public void setCode(Integer code) {
        this.code = code;
    }

    public String getMsgId() {
        return msgId;
    }

    public String getMsg() {
        return msg;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }

    public T getData() {
        return data;
    }

    public void setData(T data) {
        this.data = data;
    }
}
